/// do file for create "Table 3. Long-term Effects"
set seed 123

use "$path_data/temp/followup_student_parents_matched", replace


// variable
gen gend = q1d - 1

// PSM
local controls DT_score_pre_std_missing_0 rosen_pre_std_missing_0 cpcs_pre_std_missing_0 i.grade gend branch1 branch2 branch3 income_source1 income_source2 income_source3 income_source4 last_income_per_member hhmember hhheadage hhheadeduyear phone_survey age_tchr 

teffects psmatch (followup_cog_std) (treatment `controls')
*teffects overlap, nolabel bw(0.02)
matrix cog_psm = r(table)
scalar cog_psm_n = e(N)

teffects psmatch (RSES_std) (treatment `controls') 
matrix rses_psm = r(table)
scalar rses_psm_n = e(N)

teffects psmatch (CPCS_std) (treatment `controls') 
matrix cpcs_psm = r(table)
scalar cpcs_psm_n = e(N)

// IPWRA
teffects ipwra (followup_cog_std) (treatment `controls'), vce (cluster school_no)
matrix cog_ipwra = r(table)
scalar cog_ipwra_n = e(N)

teffects ipwra (RSES_std) (treatment `controls'), vce (cluster school_no)
matrix rses_ipwra = r(table)
scalar rses_ipwra_n = e(N)

teffects ipwra (CPCS_std) (treatment `controls') , vce (cluster school_no)
matrix cpcs_ipwra = r(table)
scalar cpcs_ipwra_n = e(N)

// AIPW
teffects aipw (followup_cog_std) (treatment `controls') , vce (cluster school_no)
matrix cog_aipw = r(table)
scalar cog_aipw_n = e(N)

teffects aipw (RSES_std) (treatment `controls') , vce (cluster school_no)
matrix rses_aipw = r(table)
scalar rses_aipw_n = e(N)

teffects aipw (CPCS_std) (treatment `controls') , vce (cluster school_no)
matrix cpcs_aipw = r(table)
scalar cpcs_aipw_n = e(N)


// Lee bounds

use "$path_data/temp/student_unbalance", replace

gen followup_cog = q6a1_correct + q6a2_correct + q6a3a_correct + q6a3b_correct + q6a4_correct + q6a5_correct

/// non-cog
// positive: 2,3,5,7,10,11,12,17,18,20,21,22,23,25,26,27,28,29,32,33,34,36,37,39
// positive-cog:1,13,14,19,24,
// negative: 4,6,8,9,30,31,35,38,40

local q99 q6c1 q6c2 q6c3 q6c4 q6c5 q6c6 q6c7 q6c8 q6c9 q6c10 q6c11 q6c12 q6c13 q6c14 q6c15 q6c16 q6c17 q6c18 q6c19 q6c20 ///
q6c21 q6c22 q6c23 q6c24 q6c25 q6c26 q6c27 q6c28 q6c29 q6c30 q6c31 q6c32 q6c33 q6c34 q6c35 q6c36 q6c37 q6c38 q6c39 q6c40 ///
q8a1a q8a2a q8a3a q8a4a q8a5a

foreach y in `q99'{
replace `y'=.  if `y'==99
}

gen noncog4 = 5 - q6c4
gen noncog6 = 5 - q6c6
gen noncog8 = 5 - q6c8
gen noncog9 = 5 - q6c9
gen noncog30 = 5 - q6c30
gen noncog31 = 5 - q6c31
gen noncog35 = 5 - q6c35
gen noncog38 = 5 - q6c38
gen noncog40 = 5 - q6c40

gen followup_noncog = q6c1+q6c2+q6c3+noncog4+q6c5+noncog6+q6c7+noncog8+noncog9+q6c10+q6c11+q6c12+q6c13+q6c14+q6c17+q6c18+q6c19+q6c20+q6c21+q6c22+q6c23+q6c24+q6c25+q6c26+q6c27+q6c28+q6c29+noncog30+noncog31+q6c32+q6c33+q6c34+noncog35+q6c36+q6c37+noncog38+noncog40+q6c39
gen followup_noncog2 = q6c2+q6c3+noncog4+q6c5+noncog6+q6c7+noncog8+noncog9+q6c10+q6c11+q6c12+q6c17+q6c18+q6c20+q6c21+q6c22+q6c23+q6c25+q6c26+q6c27+q6c28+q6c29+noncog30+noncog31+q6c32+q6c33+q6c34+noncog35+q6c36+q6c37+noncog38+noncog40+q6c39

sum followup_noncog followup_noncog2

replace followup_noncog = 190 - followup_noncog
replace followup_noncog2 = 180 - followup_noncog2

gen RSES = 40 - q6c2 - q6c3 - noncog4 - noncog6 - noncog8 - noncog9 - q6c10 - q6c11
gen CPCS = 50 - q6c2 - q6c3 - noncog4 - q6c5 - noncog6 -q6c7 - noncog8 - noncog9 - q6c10 - q6c11


/// Standardization
egen DT_score_pre_mean = mean(DT_score_pre)
egen DT_score_pre_sd = sd(DT_score_pre)
gen DT_score_pre_std = (DT_score_pre-DT_score_pre_mean)/DT_score_pre_sd
drop DT_score_pre_mean DT_score_pre_sd 

egen cpcs_pre_mean = mean(cpcs_pre)
egen cpcs_pre_sd = sd(cpcs_pre)
gen cpcs_pre_std = (cpcs_pre-cpcs_pre_mean)/cpcs_pre_sd
drop cpcs_pre_mean cpcs_pre_sd 

egen rosen_pre_mean = mean(rosen_pre)
egen rosen_pre_sd = sd(rosen_pre)
gen rosen_pre_std = (rosen_pre-rosen_pre_mean)/rosen_pre_sd
drop rosen_pre_mean rosen_pre_sd 

replace followup_cog = . if attrition == 1
replace followup_noncog = . if attrition == 1
replace CPCS = . if attrition == 1
replace RSES = . if attrition == 1

egen followup_cog_mean = mean(followup_cog)
egen followup_cog_sd = sd(followup_cog)
gen followup_cog_std = (followup_cog-followup_cog_mean)/followup_cog_sd
drop followup_cog_mean followup_cog_sd 

egen followup_noncog_mean = mean(followup_noncog)
egen followup_noncog_sd = sd(followup_noncog)
gen followup_noncog_std = (followup_noncog - followup_noncog_mean)/followup_noncog_sd
drop followup_noncog_mean followup_noncog_sd 

egen CPCS_mean = mean(CPCS)
egen CPCS_sd = sd(CPCS)
gen CPCS_std = (CPCS - CPCS_mean)/CPCS_sd
drop CPCS_mean CPCS_sd 

egen RSES_mean = mean(RSES)
egen RSES_sd = sd(RSES)
gen RSES_std = (RSES-RSES_mean)/RSES_sd
drop RSES_mean RSES_sd 

gen hyper = 1 if q7d2a == 1 & q7d2b == 2
replace hyper = 1 if q7d2a == 1 & q7d2b == 3
replace hyper = 1 if q7d2a == 2 & q7d2b == 3
gen hypernoinfo = 1 if q7d2a == .
recode hyper hypernoinfo (.=0)
replace hyper = . if hypernoinfo == 1

gen remain = 1 - attrition

leebounds followup_cog_std treatment, select(remain) vce(bootstrap, reps(1000)) 
mat cog_lee_bounds = r(table)
scalar cog_lee_bounds_n = e(N)

leebounds RSES_std treatment, select(remain) vce(bootstrap, reps(1000)) 
mat rses_lee_bounds = r(table)
scalar rses_lee_bounds_n = e(N)

leebounds CPCS_std treatment, select(remain) vce(bootstrap, reps(1000)) 
mat cpcs_lee_bounds = r(table)
scalar cpcs_lee_bounds_n = e(N)


leebounds followup_cog_std treatment, select(remain) tight(grade) vce(bootstrap, reps(1000)) 
mat cog_lee_bounds_tight = r(table)
scalar cog_lee_bounds_tight_n = e(N)

leebounds RSES_std treatment, select(remain) tight(grade) vce(bootstrap, reps(1000)) 
mat rses_lee_bounds_tight = r(table)
scalar rses_lee_bounds_tight_n = e(N)

leebounds CPCS_std treatment, select(remain) tight(grade) vce(bootstrap, reps(1000)) 
mat cpcs_lee_bounds_tight = r(table)
scalar cpcs_lee_bounds_tight_n = e(N)


/*
/// Manski bounds
manski_ci, outcome(followup_cog_std) treat(treatment) vce(bootstrap, reps(100)) 
mat cog_manski_bounds_l = e(worst)
mat cog_manski_bounds_u = e(best)

manski_ci, outcome(RSES_std) treat(treatment) vce(bootstrap, reps(100)) 
mat rses_manski_bounds_l = e(worst)
mat rses_manski_bounds_u = e(best)

manski_ci, outcome(CPCS_std) treat(treatment) vce(bootstrap, reps(100)) 
mat cpcs_manski_bounds_l = e(worst)
mat cpcs_manski_bounds_u = e(best)
*/


local outcome cog rses cpcs
local method psm ipwra aipw lee_bounds lee_bounds_tight

foreach o in `outcome'{
foreach me in `method' {
		forvalues i = 1/2 {
		if `o'_`me'[4, `i']<=0.01 {
			local star_`o'_`me'_`i' %3s "***"
		}
		else if (`o'_`me'[4, `i']>0.01) & (`o'_`me'[4, `i']<=0.05) {
			local star_`o'_`me'_`i' %2s "**"
		}
		else if (`o'_`me'[4, `i']>0.05) & (`o'_`me'[4, `i']<=0.10) {
			local star_`o'_`me'_`i' %1s "*"
		}
		else {
			local star_`o'_`me'_`i'  ""
		}
	} 
}  
} 

/// Table
tempname hh2
file open `hh2' using "$path_output/cognitive_robust.tex", write replace
file write `hh2' "" _newline
file write `hh2' "% Author: Kazuma Takakura" _newline
file write `hh2' "% Date: `c(current_date)'" _newline
file write `hh2' "% Time: `c(current_time)'" _newline
file write `hh2' "" _newline


file write `hh2' "\begin{table}[h!]\footnotesize" _newline
file write `hh2' "  \centering" _newline
file write `hh2' "  \caption{Long-term effects of the self-learning at the right level program on cognitive and non-cognitive abilities}" _newline
file write `hh2' "\label{tab:robust}" _newline
file write `hh2' "\scalebox{0.7}{" _newline
file write `hh2' "\begin{threeparttable}" _newline

file write `hh2' "\begin{tabular}{lccccccc}\toprule" _newline


file write `hh2' "  & PSM^a & IPWRA^a & AIPW^a & Lee Bound & Lee Bound & Lee Bound & Lee Bound  \\" _newline
file write `hh2' "  &  &  &   & (Lower) & (Upper) &  (Lower, Tight)^b & (Upper, Tight)^b \\\midrule\midrule" _newline

file write `hh2' "  Rapid math test score & " %04.3f (cog_psm[1,1]) `star_cog_psm_1' "  & " %04.3f (cog_ipwra[1,1]) `star_cog_ipwra_1' " & " %04.3f (cog_aipw[1,1]) `star_cog_aipw_1' " & " %04.3f (cog_lee_bounds[1,1]) `star_cog_lee_bounds_1' " & " %04.3f (cog_lee_bounds[1,2]) `star_cog_lee_bounds_2' " & " %04.3f (cog_lee_bounds_tight[1,1])  `star_cog_lee_bounds_tight_1' " & " %04.3f (cog_lee_bounds_tight[1,2]) `star_cog_lee_bounds_tight_2' "  \\ " _newline
file write `hh2' "    & ( XXX ) & (" %04.3f (cog_ipwra[2,1]) ") & (" %04.3f (cog_aipw[2,1]) ") & (" %04.3f (cog_lee_bounds[2,1]) ") & (" %04.3f (cog_lee_bounds[2,2]) ") & (" %04.3f (cog_lee_bounds_tight[2,1]) ") & (" %04.3f (cog_lee_bounds_tight[2,2]) ") \\ " _newline

file write `hh2' "  RSES score & " %04.3f (rses_psm[1,1]) `star_rses_psm_1' " & " %04.3f (rses_ipwra[1,1]) `star_rses_ipwra_1' "  & " %04.3f (rses_aipw[1,1]) `star_rses_aipw_1' " & " %04.3f (rses_lee_bounds[1,1]) `star_rses_lee_bounds_1' " & " %04.3f (rses_lee_bounds[1,2]) `star_rses_lee_bounds_2' " & " %04.3f (rses_lee_bounds_tight[1,1]) `star_rses_lee_bounds_tight_1' " & " %04.3f (rses_lee_bounds_tight[1,2]) `star_rses_lee_bounds_tight_2' "  \\ " _newline
file write `hh2' "     & ( XXX ) & (" %04.3f (rses_ipwra[2,1]) ") & (" %04.3f (rses_aipw[2,1]) ") & (" %04.3f (rses_lee_bounds[2,1]) ") & (" %04.3f (rses_lee_bounds[2,2]) ") & (" %04.3f (rses_lee_bounds_tight[2,1]) ") & (" %04.3f (rses_lee_bounds_tight[2,2]) ") \\ " _newline

file write `hh2' "  CPCS score & " %04.3f (cpcs_psm[1,1]) `star_cpcs_psm_1' " & " %04.3f (cpcs_ipwra[1,1]) `star_cpcs_ipwra_1' "  & " %04.3f (cpcs_aipw[1,1]) `star_cpcs_aipw_1' " & " %04.3f (cpcs_lee_bounds[1,1]) `star_cpcs_lee_bounds_1' " & " %04.3f (cpcs_lee_bounds[1,2]) `star_cpcs_lee_bounds_2' " & " %04.3f (cpcs_lee_bounds_tight[1,1]) `star_cpcs_lee_bounds_tight_1' " & " %04.3f (cpcs_lee_bounds_tight[1,2]) `star_cpcs_lee_bounds_tight_2' "  \\ " _newline
file write `hh2' "    & ( XXX ) & (" %04.3f (cpcs_ipwra[2,1]) ") & (" %04.3f (cpcs_aipw[2,1]) ") & (" %04.3f (cpcs_lee_bounds[2,1]) ") & (" %04.3f (cpcs_lee_bounds[2,2]) ") & (" %04.3f (cpcs_lee_bounds_tight[2,1]) ") & (" %04.3f (cpcs_lee_bounds_tight[2,2]) ") \\ " _newline


file write `hh2' "\midrule" _newline
file write `hh2' "\end{tabular}" _newline
file write `hh2' "\begin{tablenotes}" _newline
file write `hh2' "\item (a) For estimating the propensity score function and the outcome model, we use covariates including student's grade, sex, baseline cognitive and baseline non-cognitive score, DT baseline time, branch dummy (location), parents' income source, last income per family member, number of household members, age of household head, education level of household head, teacher's age, teacher's sex, and phone survey dummy." _newline
file write `hh2' "\item (c) Standard errors are reported within parentheses. For propensity score matching estimation, we calculate clustered bootstrap standard errors based on \cite{otsu2017bootstrap}. For IPWRA and AIPW, we calculate clustered standard errors. For Lee bounds estimation, we calculate bootstrap standard errors." _newline
file write `hh2' "\item (d) The numbers of observations are as follows:" (cog_psm_n) " for rapid math test score and " (rses_psm_n) " for RSES and CPCS in PSM, IPWRA, and AIPW. "(cog_lee_bounds_n) " for rapid math test score and " (rses_lee_bounds_n) " for RSES and CPCS in Lee bounds. " _newline
file write `hh2' "\item (e) $^*$ Significant at 10\% level; $^{**}$ significant at 5\% level; $^{***}$ significant at 1\% level. " _newline
file write `hh2' "\end{tablenotes}" _newline
file write `hh2' "\end{threeparttable}" _newline
file write `hh2' "}" _newline
file write `hh2' "\end{table}" _newline

file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline

file close `hh2'

